import LXCOM from '../../common/common.js'
import abspkg from '../../graph/action/absaction.js'
import {LGXEnums} from '../../common/sysenum.js'

class PolygonGraphAction extends abspkg.AbsGraphAction {
    constructor(graph) {
        super(graph);
    }

    act2Mouse(mosEvnt, worldPos) {
        var graph = this.getRefGraph();
        if (graph == null) {
            return LGXEnums.LGXPlotState.ErrorState;
        }
        var state = LGXEnums.LGXPlotState.NoneState;
        var position = graph.getPos();
        if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseDown) {
            if (graph.getStart() == 0) {
                graph.setStart(1);
                var p1 = new LXCOM.LGXPoint();
                p1.x = worldPos.x;
                p1.y = worldPos.y;
                var p2 = new LXCOM.LGXPoint();
                p2.x = worldPos.x;
                p2.y = worldPos.y;
                position.posList.push(p1);
                position.posList.push(p2);
                state = LGXEnums.LGXPlotState.AppendPtState;
            }
            else {
                var p = new LXCOM.LGXPoint();
                p.x = worldPos.x;
                p.y = worldPos.y;
                position.posList.push(p);
                state = LGXEnums.LGXPlotState.AppendPtState;
            }
        }
        else if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseDbClick
            || mosEvnt.type == LGXEnums.LGXMouseMode.RMouseUp) {
            var idx = position.posList.length - 1;
            var pt = position.posList[idx];
            pt.x = worldPos.x;
            pt.y = worldPos.y;
            graph.setFinish(1);
            state = LGXEnums.LGXPlotState.FinistState;
        }
        else if (mosEvnt.type == LGXEnums.LGXMouseMode.mouseMove
            || mosEvnt.type == LGXEnums.LGXMouseMode.mouseDrag) {
            var lst = position.posList;
            idx = lst.length - 1;
            pt = lst[idx];
            pt.x = worldPos.x;
            pt.y = worldPos.y;
            state = LGXEnums.LGXPlotState.OKState;
        }
        graph.calScope(1);
        return state;
    }
}

export default { PolygonGraphAction }
